package com.intelligent.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.intelligent.entity.Outbound;
import com.intelligent.entity.OutboundDetail;
import com.intelligent.entity.Product;
import com.intelligent.entity.Unit;
import com.intelligent.entity.Warehouse;
import com.intelligent.entity.TUser;
import lombok.extern.slf4j.Slf4j;

/**
 * Lambda缓存辅助工具类
 * 用于解决MyBatis-Plus lambda cache问题
 */
@Slf4j
public class LambdaCacheHelper {
    
    /**
     * 初始化所有相关实体类的lambda缓存
     * 通过创建LambdaQueryWrapper来触发缓存初始化
     */
    public static void initAllLambdaCache() {
        try {
            // 初始化Outbound的lambda缓存
            LambdaQueryWrapper<Outbound> outboundWrapper = new LambdaQueryWrapper<>();
            outboundWrapper.eq(Outbound::getOutboundSerialNumber, "dummy");
            outboundWrapper.orderByDesc(Outbound::getOutboundTime);
            
            // 初始化OutboundDetail的lambda缓存
            LambdaQueryWrapper<OutboundDetail> detailWrapper = new LambdaQueryWrapper<>();
            detailWrapper.eq(OutboundDetail::getOutboundNo, "dummy");
            detailWrapper.orderByDesc(OutboundDetail::getId);
            
            // 初始化Product的lambda缓存
            LambdaQueryWrapper<Product> productWrapper = new LambdaQueryWrapper<>();
            productWrapper.like(Product::getProductName, "dummy");
            productWrapper.eq(Product::getCategory, 1);
            
            // 初始化其他相关实体的lambda缓存
            LambdaQueryWrapper<Unit> unitWrapper = new LambdaQueryWrapper<>();
            unitWrapper.eq(Unit::getUnitName, "dummy");
            
            LambdaQueryWrapper<Warehouse> warehouseWrapper = new LambdaQueryWrapper<>();
            warehouseWrapper.eq(Warehouse::getWarehouseName, "dummy");
            
            LambdaQueryWrapper<TUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(TUser::getUsername, "dummy");
            
            log.info("Lambda cache initialized successfully for all entities");
        } catch (Exception e) {
            log.error("Failed to initialize lambda cache: {}", e.getMessage(), e);
            throw new RuntimeException("Lambda cache initialization failed", e);
        }
    }
    
    /**
     * 初始化出库相关实体的lambda缓存（兼容旧方法名）
     */
    public static void initOutboundLambdaCache() {
        initAllLambdaCache();
    }
    
    /**
     * 安全地创建Outbound的LambdaQueryWrapper
     * 如果出现lambda cache问题，会尝试重新初始化
     */
    public static LambdaQueryWrapper<Outbound> createOutboundWrapper() {
        try {
            return new LambdaQueryWrapper<>();
        } catch (Exception e) {
            log.warn("Lambda cache issue detected, attempting to reinitialize: {}", e.getMessage());
            initAllLambdaCache();
            return new LambdaQueryWrapper<>();
        }
    }
    
    /**
     * 安全地创建OutboundDetail的LambdaQueryWrapper
     */
    public static LambdaQueryWrapper<OutboundDetail> createOutboundDetailWrapper() {
        try {
            return new LambdaQueryWrapper<>();
        } catch (Exception e) {
            log.warn("Lambda cache issue detected for OutboundDetail, attempting to reinitialize: {}", e.getMessage());
            initAllLambdaCache();
            return new LambdaQueryWrapper<>();
        }
    }
    
    /**
     * 安全地创建Product的LambdaQueryWrapper
     */
    public static LambdaQueryWrapper<Product> createProductWrapper() {
        try {
            return new LambdaQueryWrapper<>();
        } catch (Exception e) {
            log.warn("Lambda cache issue detected for Product, attempting to reinitialize: {}", e.getMessage());
            initAllLambdaCache();
            return new LambdaQueryWrapper<>();
        }
    }
}
